var param = JSON.parse(localStorage.getItem('param'));
$(function(){
	init();
	initButton();
});
//初始化页面
function init(){
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/tea/user/addr/getUserAddrList.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			token : token,
			userId:localStorage.getItem('userid')
		},
		success : function(result) {
			console.log(result);
			if (result.retCode == '0000') {
				var addrList = result.data;
				var str = '';
				$.each(addrList,function(i,n){
					var userAddr = n.userAddr||'';
					var isDefault = n.isDefault;
					if(n.isDefault=='1'){
						str +='<div class="address-list" addressid = "'+n.id+'"><p class="address-det iphonebtn">'+n.userName+'   '+n.userPhone+'</p><p class="address-det">'+n.userArea+' '+userAddr+'</p><div class="btn-box clear"><span class="fl chose-btn chose-sel iphonebtn">默认地址</span><span class="fr del iphonebtn">删除</span><span class="fr edit iphonebtn">编辑</span></div></div>';
					}else{
						str +='<div class="address-list" addressid = "'+n.id+'"><p class="address-det iphonebtn">'+n.userName+'   '+n.userPhone+'</p><p class="address-det">'+n.userArea+' '+userAddr+'</p><div class="btn-box clear"><span class="fl chose-btn iphonebtn">设为默认</span><span class="fr del iphonebtn">删除</span><span class="fr edit iphonebtn">编辑</span></div></div>';
					}
				});
				$('.all-address').html(str);
			} 
		}
	});
}
//初始化绑定事件
function initButton(){
	//回到订单页面
	$('body').on('click','.address-det',function(){
		param.addrId = $(this).parents('.address-list').attr('addressid');
		localStorage.setItem('param',JSON.stringify(param));
		window.location = server_url + "buyNow.do";
	});
	//编辑事件
	$('body').on('click','.edit',function(){
		var addrId = $(this).parents('.address-list').attr('addressid');
		param.addrId = addrId;
		localStorage.setItem('param',JSON.stringify(param));
		window.location = server_url + "newAddress.do";
	});
	//删除事件
	$('body').on('click','.del',function(){
		var addrId = $(this).parents('.address-list').attr('addressid');
		if($(this).prev().hasClass('chose-sel')){
			alert("默认地址不能删除");
		}else{
			$.ajax({
				type : "get",
				url : server_url
				+ 'rest/tea/user/addr/deleteAddr.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					addrId:addrId
				},
				success : function(result) {
					if (result.retCode == '0000') {
						init();
					} 
				}
			});
		}
	});
	//设置默认地址
	$('body').on('click','.chose-btn', function() {
		if(!$(this).hasClass('chose-sel')) {
			//要把当前的这个设置为默认地址
			var addr = $(this);
			var addrId = $(this).parents('.address-list').attr('addressid');
			$.ajax({
				type : "get",
				url : server_url
						+ 'rest/tea/user/addr/setDefaultAddr.do?ran=' + Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					addrId:addrId,
					userId:localStorage.getItem('userid'),
				},
				success : function(result) {
					if (result.retCode == '0000') {
						addr.html('默认地址');
						addr.addClass('chose-sel').parents('.address-list').siblings().children().children('.chose-btn').removeClass('chose-sel');					
						addr.addClass('chose-sel').parents('.address-list').siblings().children().children('.chose-btn').html('设为默认')
					} 
				}
			});
			
		}
	})
	//跳转到新增列表
	$('.address-btn-box').on('click',function(){
		param.addrId = null;
		localStorage.setItem('param',JSON.stringify(param));
		window.location = server_url + "newAddress.do";
	});
}